#include "global_var.h"

channel_param   serial_channel;
QMutex  mutexUart;   //互斥量保护了串口收发的正确性。


Item_str  item645_TX[ITEMAX_TX] = {{{0x04,0x00,0x01,0x0C }, "日期、星期(其中 0 代表星期天)及时间,山东" },  //1
                              {{0x04,0x00,0x04,0x01 },"通信地址 "},  //2
                              {{0x04,0x00,0x04,0x02 },"表号"},  //3
                              {{0x04,0x00,0x04,0x03 },"资产管理编码（ASCII 码）"},  //4
                              {{0x04,0x00,0x04,0x0B },"监测单元型号（ASCII 码）"},  //5
                              {{0x04,0x00,0x04,0x0C },"生产日期（ASCII 码）"},  //6
                              {{0x04,0x00,0x04,0x0D },"协议版本号（ASCII 码）"},  //7
                              {{0x04,0x00,0x04,0x0E },"客户编号"},  //8
                              {{0x04,0x00,0x04,0x0F },"监测单元位置信息："},  //9
                              {{0x04,0x00,0x07,0x01 },"通信口 1 波特率特征字(485)"},  //10
                              {{0x04,0x00,0x07,0x02 },"通信口 2 波特率特征字(HPLC)"},  //11
                              {{0x04,0x00,0x07,0x03 },"通信口 3 波特率特征字(备用)"},  //12
                              {{0x07,0x02,0x01,0x01 },"通信口实时状态"},  //13
                              {{0x04,0x00,0x04,0xFF },"设备资产 ID 号 等信息"},  //14
                              {{0x04,0x00,0x15,0x01 },"主动上报状态字"},  //15
                              {{0x04,0x00,0x11,0x01 },"主动上报模式字"},  //16
                              {{0x07,0x06,0x01,0x01 },"被选中监测模块地址"},  //17
                     };

Item_str  item645_JC[ITEMAX] = {{{0x02,0x01,0xFF,0x00 }, "电压" },  //1
                              {{0x02,0x02,0xFF,0x00 },"电流 "},  //2
                              {{0x02,0x03,0xFF,0x00 },"瞬时有功功率 "},  //3
                              {{0x02,0x04,0xFF,0x00 },"瞬时无功功率 "},  //4
                              {{0x02,0x05,0xFF,0x00 },"瞬时视在功率 "},  //5
                              {{0x02,0x06,0xFF,0x00 }," 功率因数 "},  //6
                              {{0x04,0x00,0x01,0x0C },"山东时间 "},  //7
                     };


INT16U  bin2_int16u(INT8U bin[2])
{
   INT16U val;

   val = bin[1];
   val <<=8;
   val += bin[0];

   return val;
}
INT8U  crc16_param(INT8U *data,INT16U size)
{
    INT16U idx;
    INT8U cs;

    cs = 0;

    for(idx = 0;idx < size;idx++)
    {
        cs += data[idx];
    }

//    cs += 0xAA;

    return cs;
}
//将数据和数据长度作为参数传入,返回一个三十二位的CRC值
INT32U code_crc32(data_size* data, data_length_size length)
{
 INT32U result = 0xFFFFFFFFL;
 for (data_length_size i = 0; i < length; i++)
 {
 result = (result >> 8) ^ crc32Table[(result ^ *(data + i)) & 0xFF];
 }
 return result;
}
//将数据和数据长度作为参数传入,进行查表校验数据正确则值为0,数据错误返回0,正确返回1
bool decode_crc32(data_size* data, data_length_size length)
{
 INT32U result = 0xFFFFFFFFL;
 for (data_length_size i = 0; i < length; i++)
 {
 result = (result >> 8) ^ crc32Table[(result ^ *(data + i)) & 0xFF];
 }
 return (result ? false : true);
}
/*+++
  功能： 查找指定字符窜
----*/
INT16S  str_find(INT8U *buffer,INT16U buflen,const INT8U *target,INT16U targetlen)
{
    INT16U pos;
    INT16U idx;

    for(pos=0;pos<buflen;pos++)
    {
        for(idx=0;idx<targetlen;idx++)
        {
           if(target[idx]!=buffer[idx]) break;
        }
        if(idx>=targetlen) return pos;
        buffer++;
    }
    return -1;
}

void mem_cpy_reverse(INT8U *dst, INT8U *src, INT16U len)
{
    INT16U idx;
    for (idx=0; idx<len; idx++)
    {
        dst[idx] = src[len-idx-1];
    }
}
